const call = require('../../utils/request.js');
const util = require('../../utils/util.js');
const app = getApp();
let doctorId = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    host: call.host,
    id: 1, //当前问答的ID
    pageIndex: 1, //当前页数
    lastPageIndex: 1, //最大页面数
    question: {}, //主要问题
    list: [], //消息回复
    additional: false, //是否可追加
    nickName:'', //昵称
    avatar:'../../images/defaultHeader.png', //头像
  },

  onLoad: function (options) {
    const user = wx.getStorageSync('user');
    let obj = {
      id:options.id,
      nickName:user.nickname
    }
    if(user.avatar && user.avatar.length>0){
      obj = Object.assign(obj,{
        avatar:user.avatar
      })
    }
    this.setData(obj);
    this.getQues();
  },

  onReady: function () {

  },

  onShow: function () {
    
  },

  onHide: function () {
  },

  // 获取提问信息
  getQues() {
    wx.showLoading({
      title: '正在获取数据',
      mask:true
    })
    call.post('/api/graphic/get_questions', { group_id: this.data.id, page: this.data.pageIndex }).then((res) => {
      wx.hideLoading();
      console.log("获取提问信息", res);
      if (res.data.code == 0) {
        let data = res.data.data;
        let obj = {};
        let arr = [];
        try {
          for (let i of data.list.data) {
            arr.push({
              name: (i.initiator_identity==1?(i.name+i.position):this.data.nickName),
              photo: (i.initiator_identity==1?(data.home_domain+i.photo):(this.data.avatar)),
              content: i.content,
              img: !i.accessory ? '' : i.accessory.split(','),
              otherImg: i.accessory?3 % (i.accessory.length % 3 + 1):0,
              time: i.created_at,
              position: i.position 
            });
          }
        } catch (err) { }

        if (this.data.pageIndex == 1) { //第一页
          var accessory = data.questions.accessory;
          obj = {
            question:{
              content: data.questions.content,
              img: (!accessory)? '' : accessory.split(','),
              otherImg: accessory?3 % (accessory.length % 3 + 1):0,
              time: data.questions.created_at 
            }
          }

          var date1 = new Date(data.questions.created_at.replace(/-/g,"/"));
          var date2 = new Date();
          // console.log(date2.getTime())
          var time1 = 24 * 60 * 60 * 1000 - (date2.getTime() - date1.getTime());

          if (time1 > 0) {
            obj = Object.assign(obj,{
              additional:true
            })
            var timer = setTimeout(function () {
              this.setData({
                additional:false
              })
              clearTimeout(timer);
            }, time1);
          }
          obj = Object.assign(obj,{
            list:[arr],
            lastPageIndex:data.list.last_page
          })
        } else { //非第一页
          obj = Object.assign(obj,{
            [`list[${this.data.pageIndex}]`]:arr,
            lastPageIndex:data.list.last_page
          })
        }
        this.setData(obj);
      }
    });
  },

  // 获取下一页
  nextPage(){
    if(this.data.lastPageIndex>=this.data.pageIndex + 1){
      this.setData({
        pageIndex:this.data.pageIndex + 1
      })
      this.getQues();
    }
  },

  // 追加提问
  toChat(url) {
    wx.navigateTo({
      url: "../chat/chat?groupId=".concat(this.data.id) 
    });
  },

  //预览图片
  getPhotoClickIdx(e) {
    var _this2 = this;
    var idx = e.currentTarget.dataset.index;
    var imgs = this.data.question.img.map(function (item, index) {
      if (item.startsWith('http')) {
        return item;
      } else {
        return _this2.data.host + item;
      }
    });
    this.imgPreview(imgs, idx);
  },

  getPhotoClickList(e) {
    var _this3 = this;
    var idx = e.currentTarget.dataset.index;
    var idx2 = e.currentTarget.dataset.index2;
    var idx3 = e.currentTarget.dataset.index3;
    console.log(idx,idx2)
    var imgs = this.data.list[idx][idx2].img.map(function (item, index) {
      if (item.startsWith('http')) {
        return item;
      } else {
        return _this3.data.host + item;
      }
    });
    this.imgPreview(imgs, idx3);
  },

  imgPreview(list, idx) {
    // list：图片 url 数组
    if (list && list.length > 0) {
      wx.previewImage({
        current: list[idx], //  传 Number H5端出现不兼容
        urls: list 
      });

    }
  }
})